package com.atbeijing.read;

import com.atbeijing.bean.Teacher;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.core.search.aggregation.MaxAggregation;
import io.searchbox.core.search.aggregation.MetricAggregation;
import io.searchbox.core.search.aggregation.TermsAggregation;
import java.io.IOException;
import java.util.List;


/**

 #查名字里有"三五"且年龄大于等于20小于等于1119的第0~10位的数据,
 #根据性别聚合,查出年龄最大是多少
 GET teacher-query/_search
 {
 "query": {
 "bool": {
 "filter": {
 "range": {
 "age": {
 "gte": 20,
 "lte": 1119
 }
 }
 },
 "must": [
 {
 "match": {
 "name": "三五"
 }
 }
 ]
 }
 },
 "aggs": {
 "groupBySex": {
 "terms": {
 "field": "sex",
 "size": 100
 },
 "aggs": {
 "maxAge": {
 "max": {
 "field": "age"
 }
 }
 }
 }
 },
 "from": 0,
 "size": 10
 }

 */
public class ReadA {
    public static void main(String[] args) throws IOException {
        //1.创建jest客户端工厂
        JestClientFactory jestClientFactory = new JestClientFactory();

        //2.设置连接地址
        HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop202:9200").build();
        jestClientFactory.setHttpClientConfig(httpClientConfig);

        //3.获取连接
        JestClient jestClient = jestClientFactory.getObject();

        //4.读取数据
        Search search = new Search.Builder("{\n" +
                "  \"query\": {\n" +
                "    \"bool\": {\n" +
                "      \"filter\": {\n" +
                "        \"range\": {\n" +
                "          \"age\": {\n" +
                "            \"gte\": 20,\n" +
                "            \"lte\": 1119\n" +
                "          }\n" +
                "        }\n" +
                "      },\n" +
                "      \"must\": [\n" +
                "        {\n" +
                "          \"match\": {\n" +
                "            \"name\": \"三五\"\n" +
                "          }\n" +
                "        }\n" +
                "      ]\n" +
                "    }\n" +
                "  },\n" +
                "   \"aggs\": {\n" +
                "    \"groupBySex\": {\n" +
                "      \"terms\": {\n" +
                "        \"field\": \"sex\",\n" +
                "        \"size\": 100\n" +
                "      },\n" +
                "      \"aggs\": {\n" +
                "        \"maxAge\": {\n" +
                "          \"max\": {\n" +
                "            \"field\": \"age\"\n" +
                "          }\n" +
                "        }\n" +
                "      }\n" +
                "    }\n" +
                "  },\n" +
                "  \"from\": 0,\n" +
                "  \"size\": 10\n" +
                "}")
                .addIndex("teacher-query")
                .addType("_doc")
                .build();
        SearchResult result = jestClient.execute(search);//第一个hits

        //5.解析数据,根据kibana的结果解析
        //TODO 获取命中数据条数
        System.out.println("命中"+result.getTotal()+"条数据");

        //TODO 获取具体数据
        List<SearchResult.Hit<Teacher, Void>> hits = result.getHits(Teacher.class);//第二个hits
        for (SearchResult.Hit<Teacher, Void> hit : hits) {
            System.out.println("_index:"+hit.index);
            System.out.println("_type:"+hit.type);
            System.out.println("_id:"+hit.id);
            Teacher teacher = hit.source;
            System.out.println(teacher.toString());
            System.out.println();
        }

        System.out.println("=================================================");
        //TODO 获取聚合数据
        MetricAggregation aggregations = result.getAggregations();
        TermsAggregation groupBySex = aggregations.getTermsAggregation("groupBySex");
        //TODO 解析性别聚合组数据
        List<TermsAggregation.Entry> buckets = groupBySex.getBuckets();
        for (TermsAggregation.Entry bucket : buckets) {
            System.out.println("key:"+bucket.getKey());
            System.out.println("doc_count:"+bucket.getCount());
            //TODO 解析嵌套的maxAge聚合组数据
            MaxAggregation maxAge = bucket.getMaxAggregation("maxAge");
            System.out.println("maxAge:"+maxAge.getMax());
        }


        //关闭连接
        jestClient.shutdownClient();
    }
}
